#include "reader.h"
#include <QDebug>
#include <QtTest/QTest>

Reader::Reader(int readTime, Shelf *shelf, QObject *parent) :
    readTime(readTime), shelf(shelf), isActive(false), QObject(parent)
{
}

void Reader::activeChanged(bool active){
    if(isActive != active){
        isActive = active;
        if(isActive) emit actived();
        else emit waited();
    }
}

void Reader::run(){
    int bookIndex;
    while(true){
        while(!shelf->tryTakeBookForRead(bookIndex)){
            emit activeChange(false);
        }
        emit activeChange(true);
        QTest::qSleep(readTime*1000);
        shelf->tryPutBook(bookIndex);
    }
}
